use std::time::{Duration, Instant};


pub struct Timer {
    pub start: Instant,
    pub extra_offset_ns: i64, // 允许负数，单位：纳秒
}

impl Timer {
    pub fn new() -> Self {
        Timer { 
            start: Instant::now(),
            extra_offset_ns: 0,
        }
    }

    // pub fn add_time(&mut self, interval: Duration) {
    //     self.time = self.time + interval;
    // }

    pub fn add_time(&mut self, duration: Duration) {
        self.extra_offset_ns += duration.as_nanos() as i64;
    }

    /// 减少时间（负数）
    pub fn subtract_time(&mut self, duration: Duration) {
        self.extra_offset_ns -= duration.as_nanos() as i64;
    }

    /// 获取调整后的总时间（返回 Duration，负数截断为 0）
    pub fn total_elapsed(&self) -> Duration {
        let real_ns = self.start.elapsed().as_nanos() as i64;
        let total_ns = real_ns + self.extra_offset_ns;
        Duration::from_nanos(total_ns.max(0) as u64) // 确保非负
    }
}